<?php
ob_start();
session_start();
include "checksession.php";

date_default_timezone_set('Asia/Bangkok');
include("../connect.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Adinop Co., Ltd. was founded as an ingredients importer and local manufacturers’ distributor in 1986. We also function as an exporter of finished products</title>
		<link href="../images/it_service_logo.png" rel="shortcut icon" type="image/x-icon" />
        <link rel="stylesheet" href="../css/style.css"/>
        <script language="javascript" src="../js/jquery/jquery-1.7.1.min"></script>
        <script type="text/javascript" src="../fancybox/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
        <script type="text/javascript" src="../fancybox/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
        <link rel="stylesheet" type="text/css" href="../fancybox/fancybox/jquery.fancybox-1.3.4.css" media="screen" />
        <link rel="stylesheet" href="../fancybox/style.css" />
        <script type="text/javascript" src="autocomplete.js"></script>
        <link rel="stylesheet" href="autocomplete.css"  type="text/css"/>
        <script language="JavaScript">

            javascript:window.history.forward(1);

        </script>
        <script type="text/javascript">
            $(function() {
                var objSet = "#show_arti_topic";
                var objSetText = "ค้นหา รหัส,ชื่อ";
                if ($(objSet).val() == "") {
                    $(objSet).val(objSetText);
                }
                $(objSet).focus(function() {
                    if ($(objSet).val() == objSetText) {
                        $(objSet).val("");
                    }
                }).blur(function() {
                    if ($(objSet).val() == "") {
                        $(objSet).val(objSetText);
                    }
                });
            });
        </script>
        <script type="text/javascript">

            $(document).ready(function() {

                $('a[id^="edit"]').fancybox({
                    'width': '100%',
                    'height': '60%',
                    'autoScale': true,
                    'scrolling': 'auto',
                    'transitionIn': 'elastic',
                    'transitionOut': 'none',
                    'overlayColor': '#000',
                    'hideOnOverlayClick': false,
                    onClosed: function() {
                        parent.location.reload(true);
                    }
                });

                $('a[id^="delete"]').fancybox({
                    'width': '20%',
                    'height': '20%',
                    onStart: function() {
                        return window.confirm('คุณต้องการลบรายการนี้ไหม?');
                    },
                    onClosed: function() {
                        parent.location.reload(true);
                    }
                });

            });
        </script>
        <script language="javascript">
            function CheckValidate()
            {
                if (eval("document.frmMain.name.value") == "") {
                    alert("กรุณากรอกประเภทอุปกรณ์");
                    eval("document.frmMain.name.focus();")
                    return false;
                }
            }
        </script>
    </head>
    <body> 
        <body class="body">
            <table align="center" width="90%">
                <tr>
                    <td colspan="2" class="header" >
                        <img src="../images/logo.png"> <img src="../images/it_service_text.png" width ="150">
                            <hr/>
                    </td>
                </tr >
                <tr  valign="top">
                    <td class="left">
                        <?php include_once "left.php"; ?>
                    </td>
                    <td>



                        </div>
                        </div>
                        <?
                        $strSQL = "SELECT * FROM prd_type ";
                        $objQuery = mysql_query($strSQL) or die("Error Query [" . $strSQL . "]");
                        $Num_Rows = mysql_num_rows($objQuery);

                        $Per_Page = 7;   // Per Page

                        $Page = $_GET["Page"];
                        if (!$_GET["Page"]) {
                            $Page = 1;
                        }

                        $Prev_Page = $Page - 1;
                        $Next_Page = $Page + 1;

                        $Page_Start = (($Per_Page * $Page) - $Per_Page);
                        if ($Num_Rows <= $Per_Page) {
                            $Num_Pages = 1;
                        } else if (($Num_Rows % $Per_Page) == 0) {
                            $Num_Pages = ($Num_Rows / $Per_Page);
                        } else {
                            $Num_Pages = ($Num_Rows / $Per_Page) + 1;
                            $Num_Pages = (int) $Num_Pages;
                        }

                        $strSQL .=" order by type_id ASC LIMIT $Page_Start , $Per_Page";
                        $objQuery = mysql_query($strSQL);
                        ?>
                        <div class="nameTitle">เพิ่มประเภทอุปกรณ์</div>
                        <div align ="center">
                            <form  method="post" name ="frmMain" id="frmMain" OnSubmit="return CheckValidate();">
                                <b>เพิ่มประเภทใหม่ :</b> 
                                <input type="text" name="name" id="name"/>
                                <input type="submit" name="Submittype" id="Submittype" value="เพิ่มประเภท" />
                                <input type="hidden" name="Submittype" id="Submittype" value="Submit" />
                            </form>

                            <table width="50%"  border="0" align="center" cellpadding="1" cellspacing="1" class="simply">
                                <thead>
                                    <tr>
                                        <th width="5%">ลำดับ</th>
                                        <th width="35%" align="center">ประเภทสินค้า</div></th>
                                        <th width="5%">แก้ไข</th>
                                        <th width="5%">ลบ</th>
                                    </tr>
                                </thead>
                                <?
                                $n = 1;
                                if ($Page > 1) {
                                    $n = ($Per_Page * ($Page - 1)) + 1;
                                }
                                while ($objResult = mysql_fetch_array($objQuery)) {
                                    $type_id = $objResult[type_id];
                                    $typename = $objResult[type_name];
                                    ?>
                                    <tbody>
                                        <tr>

                                            <td width="5%"><div align="center"><?php echo $n; ?></div></td>
                                            <td width="35%"><div align="center"><?= $typename; ?></div></td>

                                            <td width="5%"><a id="edit<?= $i; ?>" class="various iframe" href="edittypeform.php?type_id=<?= $objResult["type_id"]; ?>">
                                                    <img src="../images/write.png" width="35px" ></a></td>

                                            <td width="5%">
                                                <a id="delete<?= $i; ?>" class="various iframe" href="deletetypeform.php?type_id=<?= $objResult["type_id"]; ?>">
                                                    <img src="../images/deletered.png" width="35px" ></a></td>

                                        </tr>
                                        <?
                                        $n++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                            <table width="50%"  border="0" align="center" cellpadding="1" cellspacing="1" class="simply">
                                <tr>
                                    <td>
                                        <br>
                                            Total <?= $Num_Rows; ?> Record : <?= $Num_Pages; ?> Page :
                                            <?
                                            if ($Prev_Page) {
                                                echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
                                            }

                                            for ($i = 1; $i <= $Num_Pages; $i++) {
                                                if ($i != $Page) {
                                                    echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
                                                } else {
                                                    echo "<b> $i </b>";
                                                }
                                            }
                                            if ($Page != $Num_Pages) {
                                                echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
                                            }
                                            ?>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </td>
                </tr>  
                <tr>
                    <td colspan="2">
                        <div><a href="../checklogout.php" onclick="return confirm('what do you want Exit')"> <img src="../images/logout.png" width="150px"></img></a></div>
                    </td>
                </tr>
                <tr>
                    <td class="footer" colspan="2">
                        Powered by Department Technology information Adinop
                    </td>
                </tr>
            </table>
        </body>
</html>

<?
if (isset($_POST['Submittype'])) {
    $name = $_POST['name'];
    $type_id = $_POST['type_id'];
    include "../connect.php";

    //  $sql1 = "select * from type where id_name = '$name'";
    $sql1 = "SELECT * FROM `prd_type` where `type_name` = '$name' ";
    $query_id = mysql_query($sql1);
    if (mysql_num_rows($query_id) != 0) {
        echo "<script language='javascript'>
			alert('ชื่อซ้ำครับ');
			</script>";
        echo "<meta http-equiv='refresh' content='1 ;url=typeIT.php'>";
        exit();
    }

    mysql_query("INSERT INTO prd_type VALUES('$count','$name');");
    echo "	<script language='javascript'>
			alert('บันทึกสำเร็จ');
				</script>";
    echo "<meta http-equiv='refresh' content='1 ;url=typeIT.php'>";
}
mysql_close($connection);
?>